package ru.pashkan.spring.db.dao.springJdbc;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Component;
import ru.pashkan.spring.db.dao.UserDao;
import ru.pashkan.spring.db.dao.springJdbc.mapper.UserMapper;
import ru.pashkan.spring.entity.User;

import java.util.Collection;
import java.util.List;

@Component
public class UserDaoJdbc extends AbstractSpringJdbc implements UserDao {

    private SqlParameterSource buildFullUserParameterSource( User user ) {
        return new MapSqlParameterSource()
                .addValue("user_id", user.getUserName())
                .addValue("login", user.getLogin())
                .addValue( "password", user.getPassword() )
                .addValue( "user_name", user.getUserName() )
                .addValue( "role_id", user.getRoleId() )
                .addValue( "hide", user.isHide() )
                .addValue( "registration_date", user.getRegistrationDate() );
    }

    @Override
    public void insert(User user) {
        String sql = "INSERT INTO users(login, password, user_name, role_id, hide, registration_date) " +
                "VALUES(:login, :password, :user_name, :role_id, :hide, :registration_date);";

        SqlParameterSource paramSource = buildFullUserParameterSource(user);

        this.getNamedJdbcTemplate().update(sql, paramSource);
    }

    @Override
    public void update(User user) {

    }

    @Override
    public void delete(User user) {

    }

    @Override
    public List<User> getUser(String search) {
        return null;
    }

    @Override
    public Collection<User> getAllUsers(String search) {
        return null;
    }

    @Override
    public List findUser(String login, String email) {

        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("login", login);
        parameters.addValue("email", email);

//        return this.getJdbcTemplate().query("SELECT * FROM users WHERE login IN (:login)", new UserMapper(), parameters);
        return this.getNamedJdbcTemplate().queryForList("SELECT * FROM users WHERE login IN (:login) OR email IN (:email)", parameters);

    }
    //----------------------------------
    @Override
    public User getById(Integer id) {
        return null;
    }

    @Override
    public List<User> getAll() {
        return this.getJdbcTemplate().query("SELECT * FROM users", new UserMapper());
    }

     public int getMaxUserId(){
        int userId = 0;
        List<User> user = this.getJdbcTemplate().query("SELECT MAX(user_id) FROM users", new UserMapper());
         System.out.println("======== " + user.size());
        return 6;
    }

    @Override
    public void insert() {

    }

    @Override
    public void delete(int id) {
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("userId", id);


//        return this.getJdbcTemplate().query("SELECT * FROM users WHERE login IN (:login)", new UserMapper(), parameters);
        this.getNamedJdbcTemplate().queryForList("DELETE FROM users WHERE user_id IN (:userId)", parameters);
    }
}
